package com.ruoyi.system.controller;

import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.system.domain.SysBusiness;
import com.ruoyi.system.domain.SysCancelOrderFee;
import com.ruoyi.system.domain.SysParking;
import com.ruoyi.system.domain.SysPreferential;
import com.ruoyi.system.domain.parm.SysCancelOrderFeeParm;
import com.ruoyi.system.exception.GlobalSysException;
import com.ruoyi.system.mapper.SysCancelOrderFeeMapper;
import com.ruoyi.system.service.ISysBusinessService;
import com.ruoyi.system.service.ISysCancelOrderFeeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;
import java.util.UUID;

/**
 * cancel_order_fee取消订单手续费Controller
 * 
 * @author ruoyi
 * @date 2024-04-05
 */

@Api(tags = "商家取消订单手续费设置")
@RestController
@RequestMapping("/bus/fee")
public class BusCancelOrderFeeController extends BaseController
{
    @Autowired
    private ISysCancelOrderFeeService sysCancelOrderFeeService;
    @Autowired
    private ISysBusinessService sysBusinessService;

    @Autowired
    private SysCancelOrderFeeMapper sysCancelOrderFeeMapper;
    /**
     * 查询cancel_order_fee取消订单手续费列表
     */
    @ApiOperation(value = "取消订单手续费列表")
    @GetMapping("/list")
    public TableDataInfo list(SysCancelOrderFee sysCancelOrderFee) throws Exception {

        SysBusiness bu = sysBusinessService.getUserInfo();
        if(bu==null){
            throw new Exception("用户未登陆");
        }
        sysCancelOrderFee.setBusinessId(bu.getId() );

        startPage();
        List<SysCancelOrderFee> list = sysCancelOrderFeeService.selectSysCancelOrderFeeList(sysCancelOrderFee);
        return getDataTable(list);
    }



    /**
     * 获取cancel_order_fee取消订单手续费详细信息
     */
    @ApiOperation(value = "取消订单手续费详细信息")
    @GetMapping(value = "/info")
    public AjaxResult getInfo(@RequestParam(name = "id",required = true) String  id) throws Exception {
        SysBusiness bu = sysBusinessService.getUserInfo();
        if(bu==null){
            throw new Exception("用户未登陆");
        }
        return success(sysCancelOrderFeeService.selectSysCancelOrderFeeById(id));
    }

    /**
     * 新增cancel_order_fee取消订单手续费
     */
    @PostMapping(value = "/add")
    public AjaxResult add(@RequestBody @Valid SysCancelOrderFeeParm sysCancelOrderFeeParm) throws Exception {
        SysBusiness bu = sysBusinessService.getUserInfo();
        if(bu==null){
            throw new Exception("用户未登陆");
        }
        SysCancelOrderFee sysCancelOrderFee = new SysCancelOrderFee();
        BeanUtils.copyProperties(sysCancelOrderFeeParm, sysCancelOrderFee);
        sysCancelOrderFee.setBusinessId(bu.getId() );

        // 查询价格是否用重合
        // 开始时间是否有重合
        List<SysCancelOrderFee> isHas1 = sysCancelOrderFeeMapper.selectSysCancelOrderFeeByappointmentBefor(sysCancelOrderFee.getAppointmentBefor(),bu.getId() );
        // 结束时间是否有重合
        List<SysCancelOrderFee> isHas2 = sysCancelOrderFeeMapper.selectSysCancelOrderFeeByappointmentAfter(sysCancelOrderFee.getAppointmentAfter(),bu.getId());

        if(isHas1.size()>0 || isHas2.size()>0){
            throw new GlobalSysException("定义小时范围有重合请更正");

        }

        sysCancelOrderFee.setId(UUID.randomUUID().toString());
        return toAjax(sysCancelOrderFeeService.insertSysCancelOrderFee(sysCancelOrderFee));
    }

    /**
     * 修改cancel_order_fee取消订单手续费
     */
    @PutMapping(value = "/edit")
    public AjaxResult edit(@RequestBody @Valid SysCancelOrderFeeParm sysCancelOrderFeeParm) throws Exception {
        SysBusiness bu = sysBusinessService.getUserInfo();
        if(bu==null){
            throw new Exception("用户未登陆");
        }
        if(sysCancelOrderFeeParm.getId()==null){
            throw new Exception("id不能为空");
        }
        SysCancelOrderFee sysCancelOrderFee = new SysCancelOrderFee();
        BeanUtils.copyProperties(sysCancelOrderFeeParm, sysCancelOrderFee);
        sysCancelOrderFee.setBusinessId(bu.getId() );

        // 查询价格是否用重合
        // 开始时间是否有重合
        List<SysCancelOrderFee> isHas1 = sysCancelOrderFeeMapper.selectSysCancelOrderFeeByappointmentBefor(sysCancelOrderFee.getAppointmentBefor(),bu.getId() );
        // 结束时间是否有重合
        List<SysCancelOrderFee> isHas2 = sysCancelOrderFeeMapper.selectSysCancelOrderFeeByappointmentAfter(sysCancelOrderFee.getAppointmentAfter(),bu.getId());

        if(isHas1.size()>0){
            for (SysCancelOrderFee  item:   isHas1){
                if(!item.getId().equals(sysCancelOrderFee.getId())){
                    throw new GlobalSysException("定义小时范围有重合请更正");
                }

            }
        }
        if(isHas2.size()>0){
            for (SysCancelOrderFee  item2:   isHas2){
                if(!item2.getId().equals(sysCancelOrderFee.getId())){
                    throw new GlobalSysException("定义小时范围有重合请更正");
                }

            }
        }
        return toAjax(sysCancelOrderFeeService.updateSysCancelOrderFee(sysCancelOrderFee));
    }

    /**
     * 删除cancel_order_fee取消订单手续费
     */
	@DeleteMapping("/delete/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) throws Exception {
        SysBusiness bu = sysBusinessService.getUserInfo();
        if(bu==null){
            throw new Exception("用户未登陆");
        }
        return toAjax(sysCancelOrderFeeService.deleteSysCancelOrderFeeByIds(ids,bu.getId()));
    }
}
